package com.atlin.project.service.task.impl;

import com.atlin.project.common.ErrorCode;
import com.atlin.project.contant.CommonConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.mapper.task.TaskSpacesMapper;
import com.atlin.project.model.domain.task.TaskSpaces;
import com.atlin.project.model.dto.taskSpaces.TaskSpacesQueryRequest;
import com.atlin.project.model.enums.task.TaskSpaceTypeEnum;
import com.atlin.project.model.vo.TaskSpacesVO;
import com.atlin.project.service.task.TaskSpacesService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description taskSpaces
 * @createDate date
 */
@Service
public class TaskSpacesServiceImpl extends ServiceImpl<TaskSpacesMapper, TaskSpaces>
        implements TaskSpacesService {

    @Override
    public void valid(TaskSpaces taskSpaces, boolean add) {
        String spaceType = taskSpaces.getSpaceType();
        TaskSpaceTypeEnum enumByValue = TaskSpaceTypeEnum.getEnumByValue(spaceType);
        if (enumByValue == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的空间类型");
        }
    }

    @Override
    public TaskSpacesVO getTaskSpacesVO(TaskSpaces taskSpaces, HttpServletRequest request) {
        TaskSpacesVO taskSpacesVO = TaskSpacesVO.objToVo(taskSpaces);
        // todo other operation
        return taskSpacesVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param taskSpacesQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<TaskSpaces> getQueryWrapper(TaskSpacesQueryRequest taskSpacesQueryRequest) {
        QueryWrapper<TaskSpaces> queryWrapper = new QueryWrapper<>();
        if (taskSpacesQueryRequest == null) {
            return queryWrapper;
        }
        Long id = taskSpacesQueryRequest.getId();
        String sortField = taskSpacesQueryRequest.getSortField();
        String sortOrder = taskSpacesQueryRequest.getSortOrder();
        String searchText = taskSpacesQueryRequest.getSearchText();

        String name = taskSpacesQueryRequest.getName();
        String description = taskSpacesQueryRequest.getDescription();
        String spaceType = taskSpacesQueryRequest.getSpaceType();
        Long creatorId = taskSpacesQueryRequest.getCreatorId();

        queryWrapper.like(StringUtils.isNotBlank(searchText), "name", searchText);
        queryWrapper.like(StringUtils.isNotBlank(searchText), "description", searchText);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.like(StringUtils.isNotBlank(spaceType), "spaceType", spaceType);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(creatorId), "creatorId", creatorId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public Page<TaskSpacesVO> getTaskSpacesVOPage(Page<TaskSpaces> taskSpacesPage, HttpServletRequest request) {
        List<TaskSpaces> taskSpacesList = taskSpacesPage.getRecords();
        Page<TaskSpacesVO> taskSpacesVOPage = new Page<>(taskSpacesPage.getCurrent(), taskSpacesPage.getSize(), taskSpacesPage.getTotal());
        if (CollectionUtils.isEmpty(taskSpacesList)) {
            return taskSpacesVOPage;
        }

        // 填充信息
        List<TaskSpacesVO> taskSpacesVOList = taskSpacesList.stream().map(taskSpaces -> {
            TaskSpacesVO taskSpacesVO = TaskSpacesVO.objToVo(taskSpaces);
            return taskSpacesVO;
        }).collect(Collectors.toList());
        taskSpacesVOPage.setRecords(taskSpacesVOList);
        return taskSpacesVOPage;
    }
}




